﻿if exists (select * from dbo.sysobjects 
where id = object_id(N'[dbo].[udf_OrderSubTotals]') and 
      xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[udf_OrderSubTotals]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION udf_OrderSubTotals
(	
)
-- select * from udf_OrderSubTotals()
RETURNS TABLE 
AS
RETURN 
(
  SELECT 
	[OrderDetail].OrderID, 
	Sum(Cast([UnitPrice]*[Quantity]*(1-[Discount])/100 as money)*100) AS Subtotal
	FROM [OrderDetail]
	GROUP BY [OrderDetail].OrderID
)
GO
